<?php

namespace Home\Model;

use Zend\Db\TableGateway\AbstractTableGateway;
use Zend\Db\Adapter\Adapter;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\Sql\Select;


class OnlineTable extends AbstractTableGateway {

    protected $table = 'online';
	

    public function __construct(Adapter $adapter) {
        $this->adapter = $adapter;
        $this->resultSetPrototype = new ResultSet();
        $this->resultSetPrototype->setArrayObjectPrototype(new Online());

        $this->initialize();
    }

    
	
	 public function fetchAll(Select $select = null) {
        if (null === $select)
            $select = new Select();
        $select->from($this->table,array('*'));
        $resultSet = $this->selectWith($select);
        $resultSet->buffer();
        return $resultSet;
    }
	//kiem tra ton tai
	public function check_exists($sid,$times,$timeout)
	{
		$this->delete(array('times < "'.$timeout.'"'));
		$resultSet = $this->select(array('sid'=>$sid));
    	$row = $resultSet->current();
        if($row)
		{
			$data = array(
				'times' => $times
			);
			$this->update($data, array('sid' => $sid));
		}
		else{
			$data = array(
				'times' => $times,
				'sid' => $sid
			);
			$this->insert($data);
		}
		
	}
	
	
	


}
